package ua.map
{
	import mx.core.Application;
	import mx.core.UIComponent;
	import mx.events.FlexEvent;
	import mx.messaging.ChannelSet;
	import mx.messaging.channels.AMFChannel;
	import mx.rpc.remoting.mxml.RemoteObject;
	import mx.utils.URLUtil;

	[Bindable]
	public class ServicesAS extends UIComponent
	{
		public var poiService:RemoteObject = new RemoteObject("poiService");
		public var searchService:RemoteObject = new RemoteObject("searchService");
		
		public function ServicesAS()
		{
			super();
			Application.application.addEventListener(FlexEvent.PREINITIALIZE, init);
		}
		
		public function init(e:FlexEvent):void{
			Application.application.removeEventListener(FlexEvent.ENTER_FRAME, init);
			var url:String = Application.application.url;
			var protocol:String = URLUtil.getProtocol(url);
			var server:String = URLUtil.getServerNameWithPort(url);
			if(protocol == 'file')
				server = 'localhost:8080';
			var mbURL:String= "http://" +server+ "/messagebroker/amf";
			var myAMF:AMFChannel = new AMFChannel( "my-amf", mbURL );
			var cs:ChannelSet = new ChannelSet();
			cs.addChannel(myAMF);
			
			poiService.channelSet = cs;
			searchService.channelSet = cs;
		}
	}
}